import sys
import array
from django.http import HttpResponse
from django.template import Context, loader
IMPORTPATH = "../"
if IMPORTPATH not in sys.path:
        sys.path.append(IMPORTPATH)
import mquery

myquery = mquery.Query('')

def index(request):
    t = loader.get_template("index.html")
    c = Context()
    return HttpResponse(t.render(c))

def fetch(request):
    if 'metric' in request.GET:
        metricname = str(request.GET['metric'])
    else:
        metricname = ''
    if 'start' in request.GET:
        param = str(request.GET['start'])
        start_tuple = time.strptime(param, "%Y/%m/%d-%H:%M:%S")
        start_time = int(time.mktime(start_tuple))
    else:
        start_time = 0
    if 'end' in request.GET:
        param = str(request.GET['end'])
        end_tuple = time.strptime(request, "%Y/%m/%d-%H:%M:%S")
        end_time = int(time.mktime(end_tuple))
    else:
        end_time = 0
    if 'uncompress' in request.GET:
      result = myquery.get_rdata(metricname, start_time, end_time)
    else:
      result = myquery.get_data(metricname, start_time, end_time)
    return HttpResponse(result, content_type='arraybuffer')

if __name__ == "__main__":
    class Request:
        def __init__(self):
            self.GET = {}

    request = Request()
    fetch(request)
